package com.shk.aop;



import com.shk.annotation.ExceptionHandelerGradle;
import com.shk.annotation.ExecptionHandlerAnnotation;
import com.shk.entity.Result;

import com.shk.enumerate.ExceptionGradle;
import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.stereotype.Component;


import java.lang.reflect.Method;


/**
 * 对于异常进行统一处理
 */
@Component
@Aspect
public class Aop003ExceptionHanderler {

    @Pointcut("@annotation(com.shk.annotation.ExecptionHandlerAnnotation)")
    private void pc(){}

    @Around( "pc()")
    public Object exceptionHandler( ProceedingJoinPoint pjp){

        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        ExecptionHandlerAnnotation annotation1 = method.getAnnotation(ExecptionHandlerAnnotation.class);
        Object proceed=null;

        try {
             proceed = pjp.proceed();
        } catch (Throwable throwable) {
            ExceptionHandelerGradle annotation = method.getAnnotation(ExceptionHandelerGradle.class);
            if(annotation!=null){
                ExceptionGradle value = annotation.value();
                switch (value){
                    case Safe:
                        System.out.println("我擦，错误不严重");
                        break;
                    case Keter:
                        System.out.println("问题有点严重");
                        break;
                    case Euclid:
                        System.out.println("快去解决");
                        break;
                    case Thaumiel:
                        System.out.println("删库跑路！！！！");
                        break;
                    default:
                        break;
                }
            }

            // 对于不同类型的异常分别进行处理
            Class classes = annotation1.classes();
            String mesg = annotation1.mesg();
            if(!mesg.equals("")){
                return new Result(false,mesg);

            }else {
                return new Result(false,throwable.getMessage());
            }
        }
        return proceed;


    }
}
